using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Text = TMPro.TMP_Text;
using DG.Tweening;

public class UIPlayerPanel : MonoSingleton<UIPlayerPanel> {
	private class HeartAdaper : SimpleLayout.Adapter {
		public override int count => PlayerProp.Instance == null ? 0 : PlayerProp.Instance.maxHp;
		public override void OnRender(int position, SimpleLayout.ViewCache viewCache) {
			var isOn = position < PlayerProp.Instance.hp;
			viewCache.gameObject.transform.Find("on").gameObject.SetActive(isOn);
		}
	}
	private class FoodAdapter : SimpleLayout.Adapter {
		public override int count => PlayerProp.Instance == null ? 0 : PlayerProp.Instance.maxFood;
		public override void OnRender(int position, SimpleLayout.ViewCache viewCache) {
			var isOn = position < PlayerProp.Instance.food;
			viewCache.gameObject.transform.Find("on").gameObject.SetActive(isOn);
		}
	}

	public Text playerPosText;
	public SimpleLayout heartLayout;
	public SimpleLayout foodLayout;
	public RectTransform enduranceBar;
	public RectTransform heartTransform;
	public Image mainPickaxeIcon, vicePickaxeIcon;

	public void Start() {
		heartLayout.SetAdapter(new HeartAdaper());
		heartLayout.NotifyUpdate();
	}


	private void FixedUpdate() {
		if (PlayerEntity.Instance != null) {
			var cell = TilemapDataManager.Instance.GetTilePosition(PlayerEntity.Instance.transform.position);
			playerPosText.text = $"({cell.x},{cell.y})";
		}
		heartLayout.NotifyUpdate();
		enduranceBar.SetLocalScaleX(PlayerProp.Instance.enduranceFill);
	}

	public void ShakeHp() {
		heartTransform.DOPunchScale(.25f * Vector3.one, 0.2f, vibrato: 20);
		heartTransform.DOShakeAnchorPos(0.3f, 35, vibrato: 20, fadeOut: true, randomnessMode: ShakeRandomnessMode.Harmonic);
	}
}
